package test

object bb {
  def main(args:Array[String]):Unit = {
    //匿名函数
    def fun1:Int => Int =(i:Int) => i+1
    println(fun1(1))
    val list1 = List(100,200,300).map(fun1)
    list1.foreach(println)
    val s ="abc"
    println(s.toUpperCase())
    //练习1：用匿名函数实现将一个集合wordlist中的单词全部改成大写
    val  wordList = List("java","scala","python")
    //(1)val  fun2 = (word:String) => word.toUpperCase()
    //   val  result = wordList.map(fun2)

    //(2) val  result = wordList.map (word => word.toUpperCase())
    //(3)
    val  result = wordList.map (_.toUpperCase())
    println(result)

    //map映射：返回一个元素个数与元素类是一致的新集合
    val numlist:List[Int] = List(1,2,3,4,5)
    numlist.map(t => t*2).foreach(println)
    numlist.foreach(t => println(t*2))
    numlist.map(t => if (t<4)t+1 else t).foreach(println)
    println(numlist.max)
    println(numlist.sum)

    val exam =Array(("amy",Array(60,70,45,90,88)),("tom",Array(67,48,99,75,89)))
    exam.map(tp =>(tp._1,tp._2.max)).foreach(println)

    //练习2：求其中不及格门数和平均分
    exam.map(tp =>(tp._1,tp._2.map(t =>if (t<60)1 else 0).sum)).foreach(println)
    exam.map(tp =>(tp._1,tp._2.count(_ <60))).foreach(println)
    exam.map(tp =>(tp._1,tp._2.sum * 1.0/tp._2.length)).foreach(println)
    //filter过滤
    val arr1=Array(1,2,3,4,5)
    arr1.filter(_ % 2 ==0).foreach(println)
    println("=======")
    val wordArr = Array("i","love","home")
    //过滤回文单词
    wordArr.filter(word => word == word.reverse).foreach(println)

    //flatten就是将二维压缩为一维     flatMap扁平映射
    val word = List("hello scala","hello spark","hello world")
    val wordSplit = word.map(_.split(" "))//分词
    val wordFlatten = wordSplit.flatten//扁平化
    println(wordFlatten)
    println(word.flatMap(_.split(" ")))

    //分组groupBy
    val listBy = List(1,2,3,4)
    println(listBy.groupBy(_ % 2))
    //练习：


  }



}
